import { _colors } from "../../color/index.js"
import { _polar, _random } from "../../math/index.js"
import { polarPoints } from "../../math/points/polarPoints.js"
import { pointsPath } from "../path.js"
import { _square } from "../polygon.js"
import { _g } from "../svg.js"

export const _squareFractal = (options, props) => {
    let { o = [400, 300], r = 100, a = 0, fractalLevel = 3, assemble, color = 'rd' } = options
    let n = 4
    let points = polarPoints({
        o, r, a,
        n
    })

    let children = []
    let sf = pointsPath({
        points,
        closed: true

    }, {
        stroke: 'blue',
        fill: 'none',
        ...props
    })
    children.push(sf)

    if (fractalLevel > 0) {
        let colors = _colors(color, n, 0.5)
        let sfs = points.map((t, i) => {
            let r2 = r / 2
            let o2 = assemble ? t : _polar(t, r2, a + i * 360 / n,)
            // (i+2) %n
            let a2 = a //_random(-15, 15)
            return _squareFractal({
                o: o2,
                r: r / 2,
                a: a2,
                fractalLevel: fractalLevel - 1,
                assemble,
                color
            }, {
                fill: colors[i] //i === 0 ? colors[i] : 'none' //'red'
            })

        })
        children.push(...sfs)
    }
    return _g(children)
}